package org.itstack.chain;

/**
 * 审批系统
 * 使用责任链模式处理审批请求
 */
public class ApprovalSystem {
    
    private ApprovalHandler approvalChain;
    
    public ApprovalSystem() {
        // 使用默认的审批链
        this.approvalChain = ApprovalChainBuilder.createDefaultChain();
    }
    
    public ApprovalSystem(ApprovalHandler approvalChain) {
        this.approvalChain = approvalChain;
    }
    
    /**
     * 提交审批申请
     * @param request 审批请求
     * @return 审批结果
     */
    public ApprovalResult submitRequest(ApprovalRequest request) {
        System.out.println("=== 提交审批申请 ===");
        System.out.println("申请信息：" + request);
        System.out.println();
        
        // 通过责任链处理请求
        ApprovalResult result = approvalChain.processRequest(request);
        
        System.out.println("=== 审批结果 ===");
        System.out.println("审批人：" + result.getApprover());
        System.out.println("审批结果：" + (result.isApproved() ? "通过" : "拒绝"));
        System.out.println("审批原因：" + result.getReason());
        System.out.println("审批时间：" + new java.util.Date(result.getTimestamp()));
        System.out.println();
        
        return result;
    }
    
    /**
     * 获取当前审批链信息
     */
    public void printChainInfo() {
        System.out.println("=== 审批链信息 ===");
        ApprovalHandler current = approvalChain;
        int level = 1;
        
        while (current != null) {
            System.out.println(level + ". " + current.getHandlerName());
            current = current.getNextHandler();
            level++;
        }
        System.out.println();
    }
}
